<xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- This stylesheet requires this parameter -->
<xsl:param name="character"/>
<xsl:param name="doc"/>
<xsl:param name="style"/>

<!-- output html -->
<xsl:output method="html" indent="yes"/>

<!-- If we can't find the character, present the user with a form
     containing all of the speakers in the play -->
<xsl:template name="build-character-form">

  <form method="POST" action="transform">

    <xsl:element name="input">
      <xsl:attribute name="type">hidden</xsl:attribute>
      <xsl:attribute name="name">doc</xsl:attribute>
      <xsl:attribute name="value"><xsl:value-of select="$doc"/></xsl:attribute>
    </xsl:element>
    
    <xsl:element name="input">
      <xsl:attribute name="type">hidden</xsl:attribute>
      <xsl:attribute name="name">style</xsl:attribute>
      <xsl:attribute name="value"><xsl:value-of select="$style"/></xsl:attribute>
    </xsl:element>
    
    Please select the character whose lines you would like to see:
    
    <div style="margin-left: 10pt">

      <xsl:for-each select="/PLAY/ACT/SCENE/SPEECH[not(SPEAKER=preceding::SPEAKER)]/SPEAKER">
    
        <xsl:element name="input">
          <xsl:attribute name="type">radio</xsl:attribute>
          <xsl:attribute name="name">character</xsl:attribute>
          <xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>
        </xsl:element>
        <xsl:value-of select="."/><br/>
      </xsl:for-each>

    </div>
    
    <p/>
    <input type="submit"/>

  </form>
    
</xsl:template>

<!-- override default rules -->
<xsl:template match="*|@*|text()">
   <xsl:apply-templates />
</xsl:template>

<!-- Stylesheet for play-->
<xsl:template match="/PLAY">
  <xsl:choose>
    <xsl:when test="count(/PLAY/ACT/SCENE/SPEECH[SPEAKER=$character])!=0">
      <HTML>
        <HEAD>
          <TITLE>
            <xsl:value-of select="./TITLE"/>: Character Lines for <xsl:value-of select="$character"/>
          </TITLE>
        </HEAD>
        <BODY BACKGROUND="parchment.jpg">
          <H2>Lines for the character of</H2>
          <H1><xsl:value-of select="$character"/></H1>
          <H2>in the play</H2>
          <H1><xsl:value-of select="./TITLE"/></H1>
          <H3>by William Shakespeare</H3>
    
          <xsl:apply-templates/>
    
        </BODY>
      </HTML>
    </xsl:when>
    <xsl:otherwise>
      <HTML><HEAD><TITLE>Error</TITLE></HEAD>
      <BODY BGCOLOR="white">
        <xsl:call-template name="build-character-form"/>
      </BODY>
      </HTML>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="ACT">
  <li>
    <!-- Print title of act -->
    <xsl:value-of select="./TITLE"/>
   <ul>
     <xsl:apply-templates select="./SCENE"/>
   </ul>
  </li>
</xsl:template>

<xsl:template match="SCENE">
  <li>
    <xsl:value-of select="./TITLE"/>
    <xsl:apply-templates select="SPEECH[SPEAKER=$character]"/>
  </li>
</xsl:template>

<xsl:template match="SPEECH">

<!--  <xsl:if test="./SPEAKER=$character"> -->

    <!-- Get previous speaker's last line, if there is one -->
    <p/>
    <i>
    <xsl:choose>
      <xsl:when test="preceding-sibling::SPEECH[1]">
        <b><xsl:value-of select="preceding-sibling::SPEECH[1]/SPEAKER"/></b>:
          ...<xsl:value-of select="preceding-sibling::SPEECH[1]/LINE[last()]"/>
      </xsl:when>
      <xsl:otherwise>
          (<b><xsl:value-of select="$character"/></b> opens scene)
      </xsl:otherwise>
    </xsl:choose>
    </i>

    <!-- Print line in table -->
    <table border="1">
      <tr><td>
        <b><xsl:value-of select="position()"/></b>
        <xsl:text> </xsl:text>
        <b><xsl:value-of select="$character"/>:</b><br/>
        <xsl:for-each select="./LINE">
          <xsl:value-of select="."/><br/>
        </xsl:for-each>
      </td></tr>
    </table>
    <p/>
<!--  </xsl:if> -->
</xsl:template>

</xsl:stylesheet>
  